﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.Image;
using CE.User;
using CE.Mail;

public partial class User_ResendEmailConfirmation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) ==
                Convert.ToInt32(CE.Global.CEUserType.Customer))
            {
                Response.Redirect("ViewResultHistory.aspx?ResultTitle=100");
            }
            else if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) ==
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                Response.Redirect("~/Provider/ProviderHome.aspx");
            }
        }

        //if (Session["ImageCount"] != null)
        //{
        //    if (Convert.ToInt32(Session["ImageCount"].ToString()) > 50)
        //    {
        //        lblMessage.Text = "Please try after a few moment! You are session has been locked!";
        //        btnSubmit.Enabled = false;
        //        btnNewImage.Enabled = false;
        //        txtEmail.Enabled = false;
        //        txtSecurityCode.Enabled = false;
        //        Response.Redirect("UserMessage.aspx?ErrorCode=1993");
        //    }
        //    else
        //    {
        //        lblMessage.Text = "";
        //        btnSubmit.Enabled = true;
        //        btnNewImage.Enabled = true;
        //        txtEmail.Enabled = true;
        //        txtSecurityCode.Enabled = true;
        //    }
        //}

        if (!Page.IsPostBack)
        {
            LogUserViewedHistory();
            CE.Global.General.ClearAllSession(this.Page, "ALL");
            if (Session["ImageCount"] != null)
            {
                if (Convert.ToInt32(Session["ImageCount"].ToString()) <= 5)
                {
                    CreateUpdateImageAndSession();
                }
            }
            else
            {
                CreateUpdateImageAndSession();
            }
        }
    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("PWR", Request, "User password recovery.");
    }

    private void CreateUpdateImageAndSession()
    {
        MyImage myimage = new MyImage();
        //MemoryStream memoStrem = new MemoryStream();
        string salt = "";

        if (Session[CE.Global.General.SecurityCheckWord] != null)
        {
            salt = myimage.GetRandomWord(CE.Security.MySecurity.Decrypt(Session[CE.Global.General.SecurityCheckWord].ToString(), true));
        }
        else
        {
            if (ViewState[CE.Global.General.SecurityCheckWord] != null)
            {
                salt = myimage.GetRandomWord(CE.Security.MySecurity.Decrypt(ViewState[CE.Global.General.SecurityCheckWord].ToString(), true));
            }
            else
            {
                salt = myimage.GetRandomWord("all");
            }
        }

        txtSecurityCode.MaxLength = salt.Length;
        salt = CE.Security.MySecurity.Encrypt(salt, true);
        Session[CE.Global.General.SecurityCheckWord] = salt;
        ViewState[CE.Global.General.SecurityCheckWord] = salt;
        imgCAPTCHA.ImageUrl = @"~/CAPTCHA/CAPTCHAImageHandler.ashx?id=" + salt;
        txtSecurityCode.Text = "";
    }

    protected void btnNewImage_Click(object sender, EventArgs e)
    {
        CreateUpdateImageAndSession();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //if (Page.IsValid)
        //{
        //    if (CE.Security.MySecurity.Encrypt(txtSecurityCode.Text, true) == ViewState[CE.Global.General.SecurityCheckWord].ToString())
        //    {
        //        AccountDB _accountdb = new AccountDB();
        //        Account _account = _accountdb.GetUserAccount(txtEmail.Text, Convert.ToInt32(CE.Global.CEUserType.Customer));
        //        if (_account != null)
        //        {
        //            if (_account.Status != true)
        //            {
        //                string _guid = _accountdb.GetGUID(_account.CEID);
        //                if (MyMailManager.SendConfirmationCode(_account.Email, _account.Email, _guid))
        //                {
        //                    //Response.Redirect("UserMessage.aspx?ErrorCode=300");
        //                    Response.Redirect("UserMessage.aspx?mt=UM&ec=300");
        //                    //lblMessage.Text = "Email confirmation code has been sent to your email address.";
        //                    //txtEmail.Enabled = false;
        //                    //btnSubmit.Enabled = false;
        //                    //btnNewImage.Enabled = false;
        //                }
        //            }
        //            else
        //            {
        //                lblMessage.Text = "Email address is already confirmed!";
        //                txtEmail.ControlStyle.CssClass = "validationError";
        //                CreateUpdateImageAndSession();
        //            }
        //        }
        //        else
        //        {
        //            lblMessage.Text = "Email address is not found!";
        //            txtEmail.ControlStyle.CssClass = "validationError";
        //            CreateUpdateImageAndSession();
        //        }

        //    }
        //    else
        //    {
        //        lblMessage.Text = "Security check code wrong!";
        //        txtSecurityCode.ControlStyle.CssClass = "validationError";
        //        CreateUpdateImageAndSession();
        //    }
        //}
    }

    public string CreateSalt(int size)
    {
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        Byte[] buff = new Byte[size];
        rng.GetBytes(buff);

        return Convert.ToBase64String(buff);
    }

    //public string EncrptString(string strOriginal)
    //{
    //    Byte[] clearByte;
    //    UnicodeEncoding unEncode = new UnicodeEncoding();
    //    clearByte = unEncode.GetBytes(strOriginal);
    //    HashAlgorithm hsAlgo = CryptoConfig.CreateFromName("MD5");
    //    Byte[] hsByte = hsAlgo.ComputeHash(clearByte);
    //    string strHash = BitConverter.ToString(hsByte);
    //    return strHash;
    //}

    //public string DecryptString(string strOriginal)
    //{

    //}
}
